#include "hhash.h"

#define LOGPATH	"/export/home/stock/log"
#define SIZE 1024
#define TIME_PORT 12233
#define SERVER_IP "127.0.0.1"
MYSQL *conn, mysql;

char gszdate[20];
int fnMySQLInit();
void fnMySQLDisconn();
void daemon_init();
int search_itickno( char *stockno );

typedef struct 
{
	int sockfd;
	int flags;
	char buffer[SIZE];
} cpool_t;
cpool_t cpool;
struct sockaddr_in serv_addr;

void 
cpool_init()
{  
	if(( cpool.sockfd =socket(AF_INET,SOCK_STREAM,0))< 0) lprintf(log, FATAL," cpool init socket fatal!!\n");
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr =inet_addr(SERVER_IP);
	serv_addr.sin_port = htons(TIME_PORT);  
	if (connect(cpool.sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)	
		lprintf(log, FATAL," cpool init connect fatal!!\n ");
}

/* query mysql pool */
void cpool_query( char *sql )
{
	int len;
	char buf[SIZE];
	buf[0]='\0';
	sprintf(buf,"%s;\r\n",sql);
	len=send( cpool.sockfd, buf,sizeof(buf), 0) ;
	if (len < 0) 
	{
		lprintf(log, FATAL,"len=0:%s\n", buf );
		return;
	}
	while(1)
	{
		bzero(cpool.buffer,sizeof(cpool.buffer));
		len=recv(cpool.sockfd,cpool.buffer,sizeof(cpool.buffer),0);
		if (len <= 0) 
		{
			lprintf(log, FATAL,"len<=0:%s\n", buf );
			break;
		}
		if( strstr(cpool.buffer," OK " )) break; 
		else 
		{
			lprintf( log, FATAL, "%s;\n", sql);
			break;
		}
	}
}

int 
fnMySQLInit()
{
	cpool_init();
	mysql_init( &mysql );
	if ( ( conn = mysql_real_connect( &mysql, "192.168.0.233",  "stock",  "stov2", "TSE", 0, NULL, 0 ) ) == NULL )
	{
		lprintf( log, ERROR ,"%s\n", mysql_error(&mysql));
		return 0;
	}
	else return 1;
}

void 
fnMySQLDisconn( void )
{
	mysql_close( conn );
}

int 
search_itickno( char *stockno )
{
	int i, istockno;
	for( i=0 ; i < ltick->total ; i++ )  
  	{
    		istockno=atoi( ltick->istockno[i] );
    		if( !strcmp( stockno, ltick->DIFF_num[ istockno ].stockno )) return istockno;
  	}
  	return 0;
}

void 
daemon_init()
{
	time_t tp, a;
        struct tm tmstr;
        char szLog[20], szSQL[100], szOuttmp[100];
        int itickno=0, i, t=0, serial=0;
        MYSQL_RES *result;
	MYSQL_ROW row;
        a = time( &tp );
        tmstr = *localtime( &a );
        sprintf(gszdate,"%04d%02d%02d", tmstr.tm_year+1900, tmstr.tm_mon+1, tmstr.tm_mday );
        sprintf( szLog, "%s/tail.log", LOGPATH );
        log=log_open( szLog, 0 );
        SHM_init((key_t) 1101);
        
	if( fnMySQLInit())
	{
		sprintf( szSQL, "select stock_no,openprice,max_price,min_price,new_price,add_volume from `f12` WHERE 1 AND `rec_date` = '%s' AND `deal_time ` = '13:31:00'", gszdate);
		mysql_query( conn, szSQL);
		result = mysql_use_result( conn );
        	if( result )
        	{
        		while (( row = mysql_fetch_row( result )))
  			{
  				itickno=search_itickno( row[0] );
  				if( itickno ) 
  				{
  					
  					sprintf( szSQL, "INSERT INTO TSE.f6 VALUES('%s',", gszdate );
					sprintf( szOuttmp, "'%d',", 300000 + serial  );//Transmission serial number
					serial++;
					strcat( szSQL, szOuttmp );
				
					sprintf( szOuttmp, "'%d',", itickno );  //Stock ordered number
        				strcat( szSQL, szOuttmp );
        			
        				sprintf( ltick->DIFF_num[itickno].ticktime,"13:31:00");
        				sprintf( szOuttmp, "'%s',", ltick->DIFF_num[itickno].ticktime ); //Match time
        				strcat( szSQL, szOuttmp );
        				
        				sprintf( szOuttmp, "'10000000',"); //Notes reveal the project
        				strcat( szSQL, szOuttmp );
        				
        				sprintf( szOuttmp, "'00000000',"); //	Change stop Note
        				strcat( szSQL, szOuttmp );
        				
        				sprintf( szOuttmp, "'%s',", row[4] ); //Bid
					strcat( szSQL, szOuttmp );
					
					sprintf( szOuttmp, "'%s',", row[5] ); //Volume
					strcat( szSQL, szOuttmp );
					
					for( i = 0 ; i < 10 ; i++ )
        				{
        					sprintf( szOuttmp, "'0.00',"); //Purchase price
        					strcat( szSQL, szOuttmp );
        					sprintf( szOuttmp, "'0',"); //Into the amount
        					strcat( szSQL, szOuttmp );
        				}
        				szSQL[strlen( szSQL ) - 1] = ')';
        				cpool_query( szSQL );
        				
        				
        				//insert shm
        				sscanf( row[1], "%s", &szOuttmp);
        				strcpy( ltick->DIFF_num[ itickno ].openprice, szOuttmp );//Open Price
        				
        				sscanf( row[2], "%s", &szOuttmp);
        				strcpy( ltick->DIFF_num[ itickno ].maxprice, szOuttmp );//The highest transaction price
        				
        				sscanf( row[3], "%s", &szOuttmp);
        				strcpy( ltick->DIFF_num[ itickno ].minprice, szOuttmp );//The lowest transaction price
        				
        				sscanf( row[4], "%s", &szOuttmp);
        				strcpy( ltick->DIFF_num[ itickno ].dealprice, szOuttmp );//Recent transaction price
        				
        				sscanf( row[5], "%s", &szOuttmp);
        				strcpy( ltick->DIFF_num[ itickno ].dealvolume, szOuttmp  );//Cumulative Volume
        				
					t=ltick->DIFF_num[ itickno ].num;
					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].pricediff, ltick->DIFF_num[itickno].pricediff);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].ticktime, ltick->DIFF_num[itickno].ticktime);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].serial, ltick->DIFF_num[itickno].serial);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].buyvolume, ltick->DIFF_num[itickno].buyvolume[0]);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].sellvolume, ltick->DIFF_num[itickno].sellvolume[0]);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].buyprice, ltick->DIFF_num[itickno].buyprice[0]);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].sellprice, ltick->DIFF_num[itickno].sellprice[0]);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].dealprice, ltick->DIFF_num[itickno].dealprice);
  					strcpy(ltick->DIFF_num[itickno].LDIFF_num[t].dealvolume,ltick->DIFF_num[itickno].dealvolume);
  					t++; 
  					ltick->DIFF_num[itickno].num=t;
  				}
				else
				{
					lprintf( log, ERROR ,"f12:%s\n", row[0]);
					itickno=0;
				}
   			}
   			exit(0);
   		}
   		else lprintf( log, ERROR ,"%s\n", mysql_error(conn));
		fnMySQLDisconn();
	}
}
int main()
{
	daemon_init();
	return 0;
}	